package com.lambkit.module.cms.web.user.controller;

import com.lambkit.module.cms.core.template.TemplateManager;
import com.lambkit.module.cms.rpc.model.*;
import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.rpc.model.UpmsUser;
import com.lambkit.web.controller.LambkitController;
import com.lambkit.db.sql.SqlJoinMode;
import com.lambkit.db.sql.column.Example;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresUser;

/**
 * 首页控制器
 * Created by shuzheng on 2017/3/19.
 */
@RequiresUser
public class UserIndexController extends LambkitController {

    //private static final Log LOG = Log.getLog(UserIndexController.class);

    //@RequestMapping(value = "", method = RequestMethod.GET)
    public void index() {
        // 所有系统
        Example cmsSystemExample = CmsSystem.sql().example().setOrderBy("orders asc");
        List<CmsSystem> systems = CmsSystem.service().find(cmsSystemExample);
        setAttr("systems", systems);
        // 所有类目
        Example cmsCategoryExample = CmsCategory.sql().example().setOrderBy("orders asc");
        cmsCategoryExample.join("system_id", "cms_system", "system_id", SqlJoinMode.LEFT_JOIN);
        cmsCategoryExample.setSelectSql("cms_category.*, cms_system.code, cms_system.name as sysname");
        List<CmsCategory> categories = CmsCategory.service().find(cmsCategoryExample);
        setAttr("categories", categories);
        // 所有标签
        Example cmsTagExample = CmsTag.sql().example().setOrderBy("orders asc");
        cmsTagExample.join("system_id", "cms_system", "system_id", SqlJoinMode.LEFT_JOIN);
        cmsTagExample.setSelectSql("cms_tag.*, cms_system.code, cms_system.name as sysname");
        List<CmsTag> tags = CmsTag.service().find(cmsTagExample);
        setAttr("tags", tags);
        render(TemplateManager.me().getCurrentWebPath() + "/user/index.html");
    }
    
    public void favorite() {
    	String action = getPara(0);
    	UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    	if("list".equalsIgnoreCase(action)) {
    		List<CmsFile> files = CmsFile.service().find(CmsFile.sql().andFileUserIdEqualTo(user.getUserId()).example());
        	setAttr("files", files);
    		render(TemplateManager.me().getCurrentWebPath() + "/user/favorite.html");
    	} else {
    		String id = getPara(1);
    		String type = getPara(2);
    	}
    }
    
    public void search() {
    	render(TemplateManager.me().getCurrentWebPath() + "/user/search.html");
    }
}